package com.example.shellshopping;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.fragment.app.Fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.shellshopping.login.LoginFragment;
import com.example.shellshopping.login.LoginVPAdapter;
import com.example.shellshopping.login.RegisterFragment;

import java.util.ArrayList;
import java.util.List;


public class LoginRegActivity extends AppCompatActivity {
    TextView logintv,regtv;
    ViewPager loginvp;
    List<Fragment> fragmentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_reg);
        //接收上个传来的数据，表示跳转的位置  0--登录  1--注册
        int flag = getIntent().getIntExtra("flag",0);
        initView();
        setListener();
        initPager();  //声明viewpager的展示页
        setPagerPos(flag);
    }

    //设置显示位置
    private void setPagerPos(int pos) {
        if(pos == 0){
            logintv.setTextColor(Color.rgb(241,104,173));
            regtv.setTextColor(Color.WHITE);
        }
        else if(pos==1){
            regtv.setTextColor(Color.rgb(241,104,173));
            logintv.setTextColor(Color.WHITE);
        }
        loginvp.setCurrentItem(pos);
    }

    //设置监听事件
    private void setListener() {
        logintv.setOnClickListener(OnClickListener);
        regtv.setOnClickListener(OnClickListener);
        loginvp.addOnPageChangeListener(listener);
        //initPager();  //声明viewpager的展示页
    }

    //声明viewpager的展示页
    private void initPager() {
        fragmentList = new ArrayList<>();
        fragmentList.add(new LoginFragment());
        fragmentList.add(new RegisterFragment());

        //将fragment放到viewpager里面，展示在activity上
        LoginVPAdapter vpAdapter = new LoginVPAdapter(getSupportFragmentManager(),fragmentList);
        loginvp.setAdapter(vpAdapter);  //设置适配器
    }

    ViewPager.OnPageChangeListener listener = new ViewPager.SimpleOnPageChangeListener(){
        @Override
        public void onPageSelected(int position){
            setPagerPos(position);
        }
    };

    View.OnClickListener OnClickListener= new View.OnClickListener(){
        @Override
        public void onClick(View v){
            int id = v.getId();
            switch(id){
                case R.id.login_tv_log:
                    setPagerPos(0);
                    break;
                case R.id.login_tv_reg:
                    setPagerPos(1);
                    break;
            }
        }
    };

    //初始化控件
    private void initView() {
        logintv = findViewById(R.id.login_tv_log);
        regtv = findViewById(R.id.login_tv_reg);
        loginvp = findViewById(R.id.login_vp);
    }
}